package com.qiyue.admin.auth.utils.jwt.utils;

import com.qiyue.admin.auth.configuration.JwtKeyConfiguration;
import com.qiyue.admin.auth.configuration.UserAuthConfig;
import com.qiyue.admin.auth.utils.jwt.exceotion.UserTokenException;
import com.qiyue.admin.auth.utils.model.IJWTInfo;
import com.qiyue.admin.rpc.service.PermissionService;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * Created by y747718944 on 2018/2/11
 * 集中式的jwt验证 不要eureka client
 */
@Configuration
public class JWTAuthUtils {
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    public JWTAuthUtils(
            JwtTokenUtil jwtTokenUtil) {
        this.jwtTokenUtil = jwtTokenUtil;
    }
    /**
     *  从token中获取用户信息
     * @param token
     * @return
     * @throws Exception
     */
    public IJWTInfo getInfoFromToken(String token) throws Exception {
        try {
            return jwtTokenUtil.getInfoFromToken(token);
        }catch (ExpiredJwtException ex){
            throw new UserTokenException(UserTokenException.getExceptionModel(0));
        }catch (SignatureException ex){
            throw new UserTokenException(UserTokenException.getExceptionModel(1));
        }catch (IllegalArgumentException ex){
            throw new UserTokenException(UserTokenException.getExceptionModel(2));
        }
    }
}
